//
//  MLAEateryFoodTypeVC.m
//  MagicLampApp
//
//  Created by Zheng on 2019/4/12.
//  Copyright © 2019 zheng.magic.lamp.app. All rights reserved.
//

#import "MLAEateryFoodTypeVC.h"
#import "MLAEateryFoodTypeCell.h"
#import "MLAEateryFoodTypeModel.h"
#import "MLAEateryFoodTypeEditVC.h"

@interface MLAEateryFoodTypeVC ()

@property (nonatomic, strong) NSMutableArray<MLAEateryFoodTypeModel *> *modelsArr;


@end

@implementation MLAEateryFoodTypeVC

- (void)viewDidLoad {
    [super viewDidLoad];

    [self configTitle:@"商品类型"];

    self.tableView.rowHeight = 60;
    [self.tableView registerClass:[MLAEateryFoodTypeCell class] forCellReuseIdentifier:kCellID];
    self.haveRefreshHeader = YES;

    [self configRightItemWithTitle:@"添加"];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self loadData];
}

#pragma mark - LoadData
- (void)loadData {
    [super loadData];
    
    NSDictionary *params = @{@"type":@"2"};
    
    [MLAHttpManage postHostType:MLAHttpHostType_Diancan api:kProductProductTypeList params:params success:^(id  _Nonnull response, NSString * _Nonnull msg) {
        self.modelsArr = [NSMutableArray array];
        self.page++;
        self.haveRefreshFooter = YES;
        self.modelsArr = [NSMutableArray array];
        self.modelsArr = [MLAEateryFoodTypeModel mj_objectArrayWithKeyValuesArray:response[@"lists"]];
        if (self.modelsArr.count < 20) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        
        [self.tableView reloadData];
    } failure:^{
        
    }];
    
}

- (void)loadMoreData {
    NSDictionary *params = @{@"page":@(self.page),
                             @"type":@"2"};
    [MLAHttpManage postHostType:MLAHttpHostType_Api api:kProductProductTypeList params:params success:^(id  _Nonnull response, NSString * _Nonnull msg) {
        self.page++;
        if ([response[@"lists"] count] < 20) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        } else {
            [self.tableView.mj_footer endRefreshing];
        }
        [self.modelsArr addObjectsFromArray:[MLAEateryFoodTypeModel mj_objectArrayWithKeyValuesArray:response[@"lists"]]];
        [self.tableView reloadData];
    } failure:^{
        
    }];
}


#pragma mark - tableview datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.modelsArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MLAEateryFoodTypeCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID forIndexPath:indexPath];
    MLAEateryFoodTypeModel *model = self.modelsArr[indexPath.row];
    cell.model = model;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MLAEateryFoodTypeModel *model = self.modelsArr[indexPath.row];
    MLAEateryFoodTypeEditVC *efteVC = [[MLAEateryFoodTypeEditVC alloc] init];
    efteVC.model = model;
    [self.navigationController pushViewController:efteVC animated:YES];    
    
//    @weakify(self);
//    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"操作类型" message:@"选择您的操作类型" preferredStyle:UIAlertControllerStyleActionSheet];
//    UIAlertAction *act1 = [UIAlertAction actionWithTitle:@"编辑" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//        @strongify(self);
//        [self editAction:model];
//    }];
//
//    UIAlertAction *act2 = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//        @strongify(self);
//        [self deleteAction:model];
//    }];
//
//    UIAlertAction *act3 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
//
//    [alert addAction:act1];
//    [alert addAction:act2];
//    [alert addAction:act3];
//
//    [self presentViewController:alert animated:YES completion:nil];
}

#pragma mark - Actions
//- (void)editAction:(MLAEateryFoodTypeModel *)model {
//
//}

//- (void)deleteAction:(MLAEateryFoodTypeModel *)model {
//    
//}

- (void)rightItemAction {
    MLAEateryFoodTypeEditVC *efteVC = [[MLAEateryFoodTypeEditVC alloc] init];
    [self.navigationController pushViewController:efteVC animated:YES];
}


@end
